package org.openstreetmap.travelingsalesman.gui;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import org.openstreetmap.osmosis.core.domain.v0_6.Node;
import org.openstreetmap.osmosis.core.domain.v0_6.WayNode;
import org.openstreetmap.travelingsalesman.routing.Route;
import org.openstreetmap.travelingsalesman.routing.Route.RoutingStep;

/**
 * Writes a GPX file.
 * 
 * @author Nicolas Guillaumin
 *
 */
public class GPXFileWriter {

        /**
         * XML header.
         */
        private static final String XML_HEADER = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>";
        
        /**
         * GPX opening tag
         */
        private static final String TAG_GPX = "<gpx"
                + " xmlns=\"http://www.topografix.com/GPX/1/1\""
                + " version=\"1.1\""
                + " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""
                + " xsi:schemaLocation=\"http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd \">";
        
        /**
         * Writes the GPX file
         * @param trackName Name of the GPX track (metadata)
         * @param cTrackPoints Cursor to track points.
         * @param cWayPoints Cursor to way points.
         * @param target Target GPX file
         * @throws IOException 
         */
        
        public static void writeGpxFile(String trackName, List <Node> routeNodeList, File target) throws IOException {
                FileWriter fw = new FileWriter(target);
                
                fw.write(XML_HEADER + "\n");
                fw.write(TAG_GPX + "\n");
                
                writeTrackPoints(trackName, fw, routeNodeList);
                
                fw.write("</gpx>");
                
                fw.close();
        }
        
        /**
         * Iterates on track points and write them.
         * @param trackName Name of the track (metadata).
         * @param fw Writer to the target file.
         * @param c Cursor to track points.
         * @throws IOException
         */
        //public static void writeTrackPoints(String trackName, FileWriter fw, Cursor c) throws IOException {
        private static void writeTrackPoints(String trackName, FileWriter fw, List <Node> routeNodeList) throws IOException {
                fw.write("\t" + "<trk>"+ "\n");
                fw.write("\t\t" + "<name>" + trackName + "</name>" + "\n");
                
                fw.write("\t\t" + "<trkseg>" + "\n");
                
                for (int i=0; i<routeNodeList.size(); i++)
                {
                    Node node = (Node)routeNodeList.get(i);
                    StringBuffer out = new StringBuffer();
                        out.append("\t\t\t" + "<trkpt lat=\"" 
                                        + node.getLatitude() + "\" "
                                        + "lon=\"" + node.getLongitude() + "\">");
                        out.append("</trkpt>" + "\n");
                        fw.write(out.toString());
                }
                
                fw.write("\t\t" + "</trkseg>" + "\n");
                fw.write("\t" + "</trk>" + "\n");
        }
}
